import _axios from 'axios'; 
//https://app-p1.imaiyuan.com
/*设置默认url*/
_axios.defaults.baseURL = 'https://app-p2.imaiyuan.com';

import {setRawBody,getRawBody} from './index'
/*默认头*/
const isDefString = v => 'string' === typeof v && '' !== v;
_axios.defaults.headers['raw-body'] = '';
const CancelToken = _axios.CancelToken;
const isString = v => typeof v ==='string' && v !== '';
_axios.interceptors.request.use(async (config)=>{
    const rawBody = await sessionStorage.getItem('raw-body');
    console.log(`raw-body:,ajax`, rawBody);
    if (['post', 'put', 'patch'].indexOf(config.method) != -1 && config.headers['Content-Type'] != 'application/json') {
        config.data = config.data;
      }
    console.log(`config:`, config);
    return config;
    
}), (error =>Promise.reject(error) );
// 拦截响应
_axios.interceptors.response.use(res => {
    try {
        const {
            data:{
                response:data,
                response:{
                    header:{
                        rspCode:code,
                        rspDesc: message
                    }
                }
            }
        } = res;
        return Promise.resolve({
            code,
            message,
            data,
        });
    } catch (error) {
        return Promise.reject(error);
    }
}, (error) => {
    // 针对cancel请求不做错误页处理
    if (_axios.isCancel(error)) return;
    // 401等错误的钩子
    const message = error.message;
    // 当服务器http服务崩溃的时候, 不会返回status
    const {
      status
    } = error.response || {
      status: -1
    };
    return Promise.reject(error);
  });

export default _axios
